package cn.chenchendemospringboot.order_demo.service.impl;

import cn.chenchendemospringboot.order_demo.entity.KafkaEventPublish;
import cn.chenchendemospringboot.order_demo.entity.Order;
import cn.chenchendemospringboot.order_demo.mapper.KafkaEventPublishMapper;
import cn.chenchendemospringboot.order_demo.mapper.OrderMapper;
import cn.chenchendemospringboot.order_demo.service.OrderService;
import com.google.gson.Gson;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl2 implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private KafkaEventPublishMapper kafkaEventPublishMapper;

    @Transactional(rollbackFor = Exception.class)
    public void addOrder(Order order) {
        //  新增订单
        Integer a=orderMapper.insert(order);
        // 订单数据转为json存到payLoad字段中,创建事件
        KafkaEventPublish kafkaEventPublish = new KafkaEventPublish();
        kafkaEventPublish.setStatus("New");
        kafkaEventPublish.setPayload(new Gson().toJson(order));
        kafkaEventPublish.setEventType("EVENTPUBLISH_CREATE");
        //记录待发布事件
        kafkaEventPublishMapper.insert(kafkaEventPublish);
    }
}
